package Function;
import Manu.Manu;

import java.util.*;


public class ShoppingCart {

    public static HashMap<String, Integer> goods = new HashMap<>();

    public static void shoppingCart() {
        if (goods.isEmpty()) {
            System.out.println("购物车为空");
            Manu.manu();
        } else {
            System.out.println("购物车内容：");
            for (String id : goods.keySet()) {
                int quantity = goods.get(id);
                System.out.println("商品编号：" + id + " 数量：" + quantity);
            }
        }
        Manu.manu();
    }

    public static void addGoods(String id) {
        if (goods.containsKey(id)) {
            int quantity = goods.get(id);
            goods.put(id, quantity + 1);
        } else {
            // 如果购物车不存在该商品，则将其添加，并将数量设为1
            goods.put(id, 1);
        }
        System.out.println("添加成功!");
        Manu.manu();
    }

    public static void deleteGoods(String id) {
        if (goods.containsKey(id)) {
            // 商品存在购物车中，数量减1
            int quantity = goods.get(id);
            if (quantity > 1) {
                goods.put(id, quantity - 1);
            } else {
                goods.remove(id);
            }
            System.out.println("商品删除成功！");
            Manu.manu();
        } else {
            System.out.println("购物车中不存在该商品,请重新输入！");
            Delete.delete();
        }
    }
}
